package com.learn.graph;

import com.learn.bean.Node;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 深度优先遍历
 */
public class DFS {
    public void dfs(Node node){
        if(node == null){
            return;
        }

        Queue<Node> queue = new LinkedList<>();
        HashSet<Node> set = new HashSet<>();
        queue.add(node);
        set.add(node);
        System.out.println(node.value);
        while (!queue.isEmpty()) {
            Node curNode = queue.poll();
            for (Node childNode : curNode.nexts) {
                if (!set.contains(childNode)) {
                    queue.add(curNode);
                    queue.add(childNode);
                    System.out.println(childNode.value);
                    set.add(childNode);
                    break;
                }
            }
        }
    }
}
